Completed
Push — master ( 029898...63d8f1 )
by Andres
01:05
created

reactor.js ➔ reactor   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
dl 0
loc 46
rs 8.9411
nop 5

4 Functions

Rating   Name   Duplication   Size   Complexity  
A reactor.js ➔ ... ➔ ct.isSynthesisCostMet 0 9 3
A reactor.js ➔ ... ➔ ct.synthesisMultiplier 0 4 1
A reactor.js ➔ ... ➔ ct.buySynthesis 0 12 4
A reactor.js ➔ ... ➔ synthesisPrice 0 9 2
1
'use strict';
2
3
angular.module('game').component('reactor', {
4
  templateUrl: 'views/reactor.html',
5
  controller: ['state', 'data', 'visibility', 'util', 'format', reactor],
6
  controllerAs: 'ct'
7
});
8
9
function reactor(state, data, visibility, util, format) {
10
  let ct = this;
11
  ct.state = state;
12
  ct.data = data;
13
  ct.visibility = visibility;
14
  ct.util = util;
15
  ct.format = format;
16
17
  ct.synthesisMultiplier = function (synthesis) {
18
    let level = state.player.syntheses[synthesis].number;
19
    return Math.ceil(Math.pow(data.constants.SYNTH_PRICE_INCREASE, level));
20
  };
21
22
  function synthesisPrice(synthesis) {
23
    let multiplier = ct.synthesisMultiplier(synthesis);
24
    let price = {};
25
    let reactant = data.syntheses[synthesis].reactant;
26
    for (let resource in reactant) {
27
      price[resource] = reactant[resource] * multiplier;
28
    }
29
    return price;
30
  }
31
32
  ct.isSynthesisCostMet = function (synthesis) {
33
    let price = synthesisPrice(synthesis);
34
    for (let resource in price) {
35
      if (state.player.resources[resource].number < price[resource]) {
36
        return false;
37
      }
38
    }
39
    return true;
40
  };
41
42
  ct.buySynthesis = function (synthesis, number) {
43
    let i = 0;
44
    // we need a loop since we use the ceil operator
45
    while (i < number && ct.isSynthesisCostMet(synthesis)) {
46
      let price = synthesisPrice(synthesis);
47
      for (let resource in price) {
48
        state.player.resources[resource].number -= price[resource];
49
      }
50
      state.player.syntheses[synthesis].number += 1;
51
      i++;
52
    }
53
  };
54
}
55